#ifndef OBJDB_H
#define OBJDB_H

#include <cv.h>

#include "ua_objdb/RGBHist.h"

CvHistogram*
histMsgToCv(ua_objdb::RGBHist::ConstPtr hist)
{
    CvHistogram* cvhist;
    int dims = 3;
    int sizes[3];
    float ranges[3][2];

    sizes[0] = hist->r_bins;
    sizes[1] = hist->g_bins;
    sizes[2] = hist->b_bins;

    ranges[0][0] = ranges[1][0] = ranges[2][0] = 0;
    ranges[0][1] = ranges[1][1] = ranges[2][1] = 255;

    cvhist = cvCreateHist(dims, sizes, CV_HIST_ARRAY, ranges);

    int i = 0;
    int x, y, z;
    for (x = 0; x < sizes[0]; x++) {
        for (y = 0; y < sizes[1]; y++) {
            for (z = 0; z < sizes[2]; z++) {
                float* bin = cvGetHistValue_3D(cvhist, x, y, z);
                *bin = hist->bins[i++];
            }
        }
    }

    return cvhist;
}

ua_objdb::RGBHist::Ptr
histCvToMsg(CvHistogram* cvhist)
{
    ua_objdb::RGBHist::Ptr hist(new ua_objdb::RGBHist);

    hist->r_bins = cvhist->mat.dim[0].size;
    hist->g_bins = cvhist->mat.dim[1].size;
    hist->b_bins = cvhist->mat.dim[2].size;

    int x, y, z;
    for (x = 0; x < hist->r_bins; x++) {
        for (y = 0; y < hist->g_bins; y++) {
            for (z = 0; z < hist->b_bins; z++) {
                float* bin = cvGetHistValue_3D(cvhist, x, y, z);
                hist->bins.push_back(*bin);
            }
        }
    }

    return hist;
}

#endif
